package kr.blogspot.stiner01.moduletutorial;

import java.io.IOException;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.widget.TextView;

public class MediaPlayerTest extends Activity
{
    MediaPlayer _mediaPlayer;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        TextView textView = new TextView(this);
        setContentView(textView);
        
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        _mediaPlayer = new MediaPlayer();
        try
        {
            AssetManager assetManager = getAssets();
            AssetFileDescriptor descriptor = assetManager.openFd("music.mp3");
            _mediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
            _mediaPlayer.prepare();
            _mediaPlayer.setLooping(true);
        }
        catch (IOException e)
        {
            textView.setText("Couldn't load music file, " + e.getMessage());
            _mediaPlayer = null;
        }
    }
    
    @Override
    protected void onResume()
    {
        super.onResume();
        if (_mediaPlayer != null)
        {
            _mediaPlayer.start();
        }
    }
    
    @Override
    protected void onPause()
    {
        super.onPause();
        if (_mediaPlayer != null)
        {
            _mediaPlayer.pause();
            if (isFinishing())
            {
                _mediaPlayer.stop();
                _mediaPlayer.release();
            }
        }
    }
}
